﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar.Metro;

using Bussiness_Objects;
using DataObjects.ADO;

namespace WindowsFormsApplication1
{
    public partial class EditDocGia : MetroAppForm
    {
        DataTable _tableLoaiDG = new DataTable();

        public EditDocGia(DocGia docGia)
        {
            InitializeComponent();
            loadLoaiDG();
            txtMaDG.Text = docGia.MaDocGia.ToString();
            txtTenDG.Text = docGia.HoTen;
            cmbLoaiDG.SelectedValue = docGia.MaLoaiDocGia;
            dtimeNgaySinh.Text = docGia.NgaySinh;
            txtEmail.Text = docGia.Email;
            txtDiaChi.Text = docGia.DiaChi;
            dtimeNgayLT.Text = docGia.NgayLapThe;
            txtNgayHetHan.Text = docGia.NgatHetHan.Split(' ')[0];

            lbl_Error_Tuoi.Visible = false;
            pic_error_Email.Visible = false;
        }

        private void loadLoaiDG()
        {
            _tableLoaiDG = (new SqlLoaiDocGia()).GetTable();

            cmbLoaiDG.DataSource = _tableLoaiDG;
            cmbLoaiDG.DisplayMember = _tableLoaiDG.Columns[1].ToString();
            cmbLoaiDG.ValueMember = _tableLoaiDG.Columns[0].ToString();  
        }

        private void butCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }                

        private void txtEmail_Leave(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtEmail.Text))
            {
                if (!Bussiness_Objects.BusinessRule.ValidateEmail.CheckEmail(txtEmail.Text))
                {
                    pic_error_Email.Visible = true;
                    txtEmail.Focus();
                }
                else
                    pic_error_Email.Visible = false;
            }
        }

        private void dtimeNgayLT_Leave(object sender, EventArgs e)
        {
            txtNgayHetHan.Text = MainForm._ThamSo.ThoiHanTHe(dtimeNgayLT.Text);
        }

        private void dtime_NgaySinh_Leave(object sender, EventArgs e)
        {
            if (!MainForm._ThamSo.KiemTraTuoi(dtimeNgaySinh.Text))
            {
                lbl_Error_Tuoi.Text = MainForm._ThamSo.KiemTraTuoi_Error;
                lbl_Error_Tuoi.Visible = true;
            }
            else
                lbl_Error_Tuoi.Visible = false;
        }

        private void txtTenDG_Leave(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtTenDG.Text))
            {
                txtTenDG.Text = Bussiness_Objects.BusinessRule.ValiadateText.UpcaseName(txtTenDG.Text);
            }
        }

        private void txtDiaChi_Leave(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtDiaChi.Text))
            {
                txtDiaChi.Text = Bussiness_Objects.BusinessRule.ValiadateText.TrimText(txtDiaChi.Text);
            }
        }

        private void butOK_Click(object sender, EventArgs e)
        {            
            DocGia docGia = new DocGia(
                int.Parse(txtMaDG.Text),
                (int)cmbLoaiDG.SelectedValue,
                txtTenDG.Text,
                dtimeNgaySinh.Text,
                txtDiaChi.Text,
                txtEmail.Text,
                dtimeNgayLT.Text,
                null,
                0);
            MainForm._formThongTinDG.UpdateDocGia(docGia);            
        }
    }
}
